package org.eredlab.g4.lz.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.arm.util.idgenerator.IDHelper;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.ccl.util.G4Utils;
import org.eredlab.g4.lz.service.StockService;
import org.eredlab.g4.rif.util.SessionContainer;
import org.eredlab.g4.rif.util.WebUtils;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

public class StockAction extends BaseAction {

	/**
	 * service
	 */
	private StockService stockservice = (StockService) getService("StockService");

	/**
	 * 查询所有商品
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryDepts(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List areaList = g4Reader.queryForList("lz.deptData", dto);
		String jsonString = JsonHelper.encodeObject2Json(areaList);
		write(jsonString, response);
		return mapping.findForward(null);
	}

	/**
	 * 期初初始查询
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward periodInit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("periodInit");
	}

	/**
	 * 查询期初订单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryPeriod(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.perstock_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"lz.perstock_select_1_Count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	/**
	 * 审核期初订单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward auitPeriod(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Dto re = new BaseDto();
		SessionContainer sessionContainer = WebUtils
				.getSessionContainer(request);
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		String stockno = dto.getAsString("stockno");
		if (stockno == null || "".equals(stockno)) {
			re.put("result", "操作失败");
		} else {
			// 更改数据
			// 添加库存
			Dto outDto = (Dto) g4Reader.queryForObject("lz.perstock_select_2",
					dto);
			if (outDto != null) {
				Dto newDto = (Dto) g4Reader.queryForObject("lz.stock_select_1",
						outDto);
				if (newDto != null && newDto.get("instockno") != null
						&& !"".endsWith(newDto.getAsString("instockno"))) {
					outDto.put("instockno", newDto.getAsString("instockno"));
					outDto.put("updateTime", G4Utils.getCurrentTimestamp());// 建立时间
																			// 当前时间
					outDto.put("stockoper", sessionContainer.getUserInfo()
							.getUserid());// 操作人姓名
					outDto.put("stocknum", outDto.getAsInteger("instocknum")
							+ newDto.getAsInteger("stocknum"));
					outDto.put("instocknum", outDto.getAsInteger("instocknum")
							+ newDto.getAsInteger("instocknum"));
					stockservice.updateStore(outDto);
					re.put("result", "操作成功");
				} else {
					newDto = new BaseDto();
					outDto.put("instockno", IDHelper.getGoodId("SK", 0));
					outDto.put("createTime", G4Utils.getCurrentTimestamp());// 建立时间
																			// 当前时间
					outDto.put("stockoper", sessionContainer.getUserInfo()
							.getUserid());// 操作人姓名
					// outDto.put("stocknum",
					// outDto.getAsInteger("instocknum"));
					stockservice.insertStore(outDto);
					re.put("result", "操作成功");
				}

			} else {
				re.put("result", "操作失败");
			}

		}
		super.write(re.toJson(), response);
		return mapping.findForward(null);
	}

	/**
	 * 审核期初订单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward auitPeriod2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Dto re = new BaseDto();
		SessionContainer sessionContainer = WebUtils
				.getSessionContainer(request);
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		String stockno = dto.getAsString("stockno");
		if (stockno == null || "".equals(stockno)) {
			setOkTipMsg("操作失败", response);
		} else {
			// 更改数据
			Dto outDto = (Dto) g4Reader.queryForObject("lz.perstock_select_2",
					dto);
			if (outDto != null) {
				Dto addDto = null;
				if (dto.getAsInteger("instocknum") < outDto
						.getAsInteger("instocknum")) {
					addDto = new BaseDto();
					addDto.put("stockno", IDHelper.getGoodId("QC", 0));// 建立时间
																		// 当前时间
					addDto.put("createTime", G4Utils.getCurrentTimestamp());// 建立时间
																			// 当前时间
					addDto.put("opernames", sessionContainer.getUserInfo()
							.getUserid());// 操作人姓名
					addDto.put("stockTypes", 0);// 操作人姓名
					addDto.put("orderno", outDto.getAsString("orderno"));
					addDto.put("detailno", outDto.getAsString("detailno"));
					addDto.put("goodno", outDto.getAsString("goodno"));
					addDto.put("ordernum", outDto.getAsInteger("instocknum")
							- dto.getAsInteger("instocknum"));
					addDto.put("orders", outDto.getAsString("stockschool"));
				} else if (dto.getAsInteger("instocknum") > outDto
						.getAsInteger("instocknum")) {
					setOkTipMsg("操作数量超出范围", response);
					super.write(re.toJson(), response);
					return mapping.findForward(null);
				}

				Dto newDto = (Dto) g4Reader.queryForObject("lz.stock_select_1",
						outDto);
				if (newDto != null && newDto.get("instockno") != null
						&& !"".endsWith(newDto.getAsString("instockno"))) {
					outDto.put("instockno", newDto.getAsString("instockno"));
					outDto.put("updateTime", G4Utils.getCurrentTimestamp());// 建立时间
																			// 当前时间
					outDto.put("stockoper", sessionContainer.getUserInfo()
							.getUserid());// 操作人姓名
					outDto.put("stocknum", dto.getAsInteger("instocknum")
							+ newDto.getAsInteger("stocknum"));
					outDto.put("instocknum", dto.getAsInteger("instocknum")
							+ newDto.getAsInteger("instocknum"));
					outDto.put("createnum", dto.getAsInteger("instocknum"));
					setOkTipMsg("操作成功", response);
					stockservice.insertStore2(outDto, addDto, false);
				} else {
					newDto = new BaseDto();
					outDto.put("instockno", IDHelper.getGoodId("SK", 0));
					outDto.put("createTime", G4Utils.getCurrentTimestamp());// 建立时间
					outDto.put("goodno", outDto.getAsString("goodno"));														// 当前时间
					outDto.put("stockoper", sessionContainer.getUserInfo()
							.getUserid());// 操作人姓名
					outDto.put("instocknum", dto.getAsInteger("instocknum"));
					outDto.put("stocknum", dto.getAsInteger("instocknum"));
					outDto.put("createnum", dto.getAsInteger("instocknum"));
					setOkTipMsg("操作成功", response);
					stockservice.insertStore2(outDto, addDto, true);
				}

			} else {
				setOkTipMsg("操作失败", response);
			}

		}
		// super.write(re.toJson(), response);
		return mapping.findForward(null);
	}

	/**
	 * 库存查询
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward stockInit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("stockInit");
	}

	/**
	 * 查询库存
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.stock_select_2", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"lz.stock_select_2_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}

	/**
	 * 审核期初订单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SessionContainer sessionContainer = WebUtils
				.getSessionContainer(request);
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		int innum = dto.getAsInteger("innum");
		if (innum > 0) {
			// 更改数据
			// 添加库存
			dto.put("goodno", dto.getAsString("ingood"));
			dto.put("stockschool", dto.getAsString("inschool"));
			dto.put("ordernum", innum);
			Dto newDto = (Dto) g4Reader.queryForObject("lz.stock_select_1",dto);
			
			if (newDto != null && newDto.get("instockno") != null
					&& !"".endsWith(newDto.getAsString("instockno"))) {
				newDto.put("updateTime", G4Utils.getCurrentTimestamp());// 建立时间
				newDto.put("stockoper", sessionContainer.getUserInfo().getUserid());// 操作人姓名
				newDto.put("stocknum", innum+ newDto.getAsInteger("stocknum"));
				newDto.put("instocknum", innum+ newDto.getAsInteger("instocknum"));
				newDto.put("createTime", G4Utils.getCurrentTimestamp());// 建立时间
				newDto.put("ordernum", innum);
				newDto.put("stockschool",  dto.getAsString("inschool"));
				newDto.put("cstockno", IDHelper.getGoodId("CK", 0));
				stockservice.addStock(newDto,false);
				setOkTipMsg("操作成功", response);
			} else {
				newDto = new BaseDto();
				newDto.put("instockno", IDHelper.getGoodId("SK", 0));
				newDto.put("createTime", G4Utils.getCurrentTimestamp());// 建立时间
				newDto.put("stocknum", innum);
				newDto.put("instocknum", innum);
				newDto.put("goodno", dto.getAsString("ingood"));// 当前时间
				newDto.put("stockoper", sessionContainer.getUserInfo()
						.getUserid());// 操作人姓名
				newDto.put("ordernum", innum);
				newDto.put("stockschool",  dto.getAsString("inschool"));
				newDto.put("cstockno", IDHelper.getGoodId("CK", 0));
				stockservice.addStock(newDto,true);
				setOkTipMsg("操作成功", response);
			}
		}else{
			setOkTipMsg("新建数量小于0", response);
		}
		return mapping.findForward(null);
	}
	
	/**
	 * 库存查询
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward cstockInit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("cstockInit");
	}

	/**
	 * 查询库存
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward queryCStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		List list = g4Reader.queryForPage("lz.cstock_select_1", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject(
				"lz.cstock_select_1_count", dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger,
				G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
}
